package com.pearadmin.common.secure.domain;

import com.pearadmin.common.secure.dto.JwtUserDto;
import com.pearadmin.modules.sys.domain.SysPower;
import com.pearadmin.modules.sys.domain.SysRole;
import com.pearadmin.modules.sys.domain.SysUser;
import com.pearadmin.modules.sys.domain.SysUserRole;
import com.pearadmin.modules.sys.mapper.SysPowerMapper;
import com.pearadmin.modules.sys.mapper.SysRoleMapper;
import com.pearadmin.modules.sys.mapper.SysUserMapper;
import com.pearadmin.modules.sys.mapper.SysUserRoleMapper;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Describe: Security 用户服务
 * Author: 就 眠 仪 式
 * CreateTime: 2019/10/23
 */
@Component
public class SecureUserDetailsServiceImpl implements UserDetailsService {

    @Resource
    private SysUserMapper sysUserMapper;

    @Resource
    private SysPowerMapper sysPowerMapper;

    @Resource
    private SysUserRoleMapper sysUserRoleMapper;

    @Resource
    private SysRoleMapper sysRoleMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUser sysUser = sysUserMapper.selectByUsername(username);
        if (sysUser == null) {
            throw new UsernameNotFoundException("用户未找到");
        }
        List<GrantedAuthority> grantedAuthorities = new ArrayList<>();
        List<SysPower> powerList = sysPowerMapper.selectByUsername(username);
        List<String> collect = powerList.stream().map(SysPower::getPowerCode).collect(Collectors.toList());
        for (String authority : collect) {
            if (!("").equals(authority) & authority !=null){
                GrantedAuthority grantedAuthority = new SimpleGrantedAuthority(authority);
                grantedAuthorities.add(grantedAuthority);
            }
        }
        //将用户所拥有的权限加入GrantedAuthority集合中
        JwtUserDto loginUser =new JwtUserDto(sysUser,grantedAuthorities);
        loginUser.setRoleInfo(getRoleInfo(sysUser));
        sysUser.setPowerList(powerList);
        return sysUser;
    }

    public List<SysRole> getRoleInfo(SysUser myUser) {
        SysUser userByName = sysUserMapper.selectByUsername(myUser.getUsername());
        List<SysUserRole> roleUserByUserId = sysUserRoleMapper.selectByUserId(userByName.getUserId());
        List <SysRole> roleList = new ArrayList<>();
        for (SysUserRole roleUser:roleUserByUserId){
            String roleId = roleUser.getRoleId();
            SysRole roleById = sysRoleMapper.selectById(roleId);
            roleList.add(roleById);
        }
        return roleList;
    }
}
